#include "list.h"

list* createList(void* data)
{
  list* l = malloc(sizeof(list));
  l->next = NULL;
  l->data = data;
  return l;
} 

list* addElementToList(void* data, list* l)
{
  list* element = malloc(sizeof(list));
  element->data = data;
  element->next = l;
  return element;
}

list* removeElementList(list* l)
{
  list* temp = l->next;
  free(l);
  if (temp == NULL)
    printf("WARNING : list is NULL after removeElementList\n");
  return temp;
}

list* removeGivenElementList(void* data, list* l)
{
  assert(l != NULL);
  list* start = l;
  while(l->data != data && l != NULL)
    l = l->next;
  if (l != NULL)
    removeElementList(l);
  else
    printf("ERREUR : Impossible de trouver l'élément dans la liste\n");
  return start;
}

list* addElementToListLast(void* data, list* l)
{
  if (l == NULL)
    {
      return addElementToList(data,NULL);
    }
  else
    {
      list* start = l;
      list* temp = l->next;
      while(temp != NULL)
	{
	  l = temp;
	  temp = temp->next;
	}
      list* element = malloc(sizeof(list));
      element->data = data;
      element->next = NULL;
      l->next = element;
      return start;
    }
}

void dumpList(list* l)
{
  printf("DUMPING LIST\n");
  int i = 0;
  while(l != NULL)
    {
      printf("Element %d : %p\n", i++, l->data);
      l = l->next;
    }
}
